package dl.bukkit.myststus.vexshow.sub;

import dl.bukkit.myststus.configuration.imp.Config;
import dl.bukkit.myststus.hook.PlaceholderAPIHook;
import dl.bukkit.myststus.library.ats.GVexHud;
import dl.bukkit.myststus.utils.Calculator;
import dl.bukkit.myststus.utils.StringUtils;
import dl.bukkit.myststus.vexshow.AbstractVexHud;
import lk.vexview.hud.VexShow;
import lk.vexview.hud.VexTextShow;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

import java.util.Collections;
import java.util.List;

@GVexHud(
        folder = "text",
        author = "DogLong"
)
public class Text extends AbstractVexHud {

    private final double scale;
    private final int length;
    private final String text;
    private final String delText;
    private final String expression;

    public Text(String k, YamlConfiguration section) {
        super(k, section);
        this.text = section.getString("text");
        this.delText = StringUtils.delAllColors(text);
        this.scale = section.getDouble("scale");
        this.length = section.getInt("length");
        this.expression = section.getString("expression", "none");
    }

    public double getLength(Player player) {
        int length = PlaceholderAPIHook.toString(player, delText).length() - this.length;
        if (length > 0) {
            return  Calculator.isExpression(this.expression.replaceAll(
                    "\\{" + Config.getInst().getString("Placeholder.TextFontNumber") + "}", String.valueOf(length)))
                    ? Calculator.executeExpression(this.expression.replaceAll(
                    "\\{" + Config.getInst().getString("Placeholder.TextFontNumber") + "}", String.valueOf(length))) : 0;
        }
        return 0;
    }

    @Override
    public List<VexShow> getVexShowList(Player player, Entity target) {
        player.sendMessage(String.valueOf(length));
        return Collections.singletonList(new VexTextShow(getId(), getX(player), getY(player), getZ(), Collections.singletonList(text), 0, scale));
    }
}
